package grpc

import (
	"context"
	"fmt"
	"github.com/dgrijalva/jwt-go"
	"math/rand"
	"short-video/rpc/global"
	"short-video/rpc/models/dao"
	"short-video/rpc/models/services"
	"short-video/rpc/pkg"
	user1 "short-video/rpc/proto/User"
	"strconv"
	"time"
)

type UserServer struct {
	user1.UnimplementedRegisterServerServer
}

func (s *UserServer) Register(ctx context.Context, req *user1.RegisterReq) (*user1.RegisterResp, error) {
	if !pkg.ValidateMobile(req.Mobile) {
		return &user1.RegisterResp{
			Code: 400,
			Msg:  "手机格式错误",
		}, nil
	}
	user := dao.User{
		Name:   req.Name,
		Mobile: req.Mobile,
		Pass:   req.Pass,
	}
	err := services.CrateUser(user)
	if err != nil {
		return &user1.RegisterResp{
			Code: 400,
			Msg:  "注册用户失败",
		}, err
	}
	resp := &user1.RegisterResp{
		Code: 200,
		Msg:  "注册用户成功",
	}
	return resp, err
}

func (s *UserServer) GetCache(ctx context.Context, req *user1.GetCacheReq) (*user1.GetCacheResp, error) {
	if !pkg.ValidateMobile(req.Mobile) {
		return &user1.GetCacheResp{
			Code: 400,
			Msg:  "手机格式错误",
		}, nil
	}
	key := fmt.Sprintf("login%s", req.Mobile)
	code := rand.Intn(9000) + 1000
	codeStr := strconv.Itoa(code)
	err := global.RedisClient.Set(key, codeStr, time.Minute*10).Err()
	if err != nil {
		return &user1.GetCacheResp{
			Code: 400,
			Msg:  "获取缓存失败",
		}, err
	}
	resp := &user1.GetCacheResp{
		Code: 200,
		Msg:  "获取缓存成功",
	}
	return resp, err
}

func (s *UserServer) Login(ctx context.Context, req *user1.LoginReq) (*user1.LoginResp, error) {
	if !pkg.ValidateMobile(req.Mobile) {
		return &user1.LoginResp{
			Code: 400,
			Msg:  "手机格式错误",
			Data: nil,
		}, nil
	}
	res, err := services.QueryUserByMobile(req.Mobile)
	if err != nil {
		return &user1.LoginResp{
			Code: 400,
			Msg:  "用户不存在",
			Data: nil,
		}, err
	}
	CustomClaims := pkg.CustomClaims{
		ID: res.ID,
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400,
		},
	}
	token, err := pkg.NewJWT("2211A").CreateToken(CustomClaims)
	if err != nil {
		return &user1.LoginResp{
			Code: 400,
			Msg:  "登录失败",
			Data: nil,
		}, err
	}
	resp := &user1.LoginResp{
		Code: 200,
		Msg:  "登录成功",
		Data: &user1.TokenData{Token: token},
	}
	return resp, err
}

func (s *UserServer) AddHomePage(ctx context.Context, req *user1.AddHomePageReq) (*user1.AddHomePageResp, error) {
	page := dao.HomePage{
		Name:               req.Name,
		Address:            req.Address,
		FollowWithInterest: int(req.FollowWithInterest),
		Fans:               int(req.Fans),
		GetThumbsUp:        int(req.GetThumbsUp),
		Works:              int(req.Works),
		Hobby:              int(req.Hobby),
	}
	err := services.CrateHomePage(page)
	if err != nil {
		return &user1.AddHomePageResp{
			Code: 400,
			Msg:  "创建失败",
		}, err
	}
	resp := &user1.AddHomePageResp{
		Code: 200,
		Msg:  "创建成功",
	}
	return resp, err
}

func (s *UserServer) UserInfo(ctx context.Context, req *user1.UserInfoReq) (*user1.UserInfoResp, error) {
	list, err := services.QueryUserInfoByName(req.Name)
	if err != nil {
		return &user1.UserInfoResp{
			Code: 400,
			Msg:  "列表展示失败",
			Data: nil,
		}, err
	}
	var user []*user1.UserInfo
	for _, v := range list {
		user = append(user, &user1.UserInfo{
			Id:                 int64(v.ID),
			Name:               v.Name,
			Address:            v.Address,
			FollowWithInterest: int64(v.FollowWithInterest),
			Fans:               int64(v.Fans),
			GetThumbsUp:        int64(v.GetThumbsUp),
			Works:              int64(v.Works),
			Hobby:              int64(v.Hobby),
		})
	}
	resp := &user1.UserInfoResp{
		Code: 200,
		Msg:  "列表展示成功",
		Data: user,
	}
	return resp, err
}

func (s *UserServer) AddWorks(ctx context.Context, req *user1.AddWorksReq) (*user1.AddWorksResp, error) {
	work := dao.Works{
		Title:         req.Title,
		Music:         req.Music,
		Video:         req.Video,
		Picture:       req.Picture,
		SubjectOfTalk: req.SubjectOfTalk,
		Types:         req.Types,
	}
	err := services.CrateWorks(work)
	if err != nil {
		return &user1.AddWorksResp{
			Code: 400,
			Msg:  "创建失败",
		}, err
	}
	resp := &user1.AddWorksResp{
		Code: 200,
		Msg:  "创建成功",
	}
	return resp, err
}

func (s *UserServer) WorksList(ctx context.Context, req *user1.WorksListReq) (*user1.WorksListResp, error) {
	title, err := services.QueryWorksInfoByTitle(req.Title)
	if err != nil {
		return &user1.WorksListResp{
			Code: 400,
			Msg:  "展示失败",
			Data: nil,
		}, err
	}
	var worksList []*user1.WorksInfo
	for _, v := range title {
		worksList = append(worksList, &user1.WorksInfo{
			Id:            int64(v.ID),
			Title:         v.Title,
			Music:         v.Music,
			Video:         v.Video,
			Picture:       v.Picture,
			SubjectOfTalk: v.SubjectOfTalk,
			Types:         v.Types,
		})
	}
	resp := &user1.WorksListResp{
		Code: 200,
		Msg:  "展示成功",
		Data: worksList,
	}
	return resp, err
}

func (s *UserServer) AddGiveThumbsUp(ctx context.Context, req *user1.AddGiveThumbsUpReq) (*user1.AddGiveThumbsUpResp, error) {
	up := dao.GiveThumbsUp{
		UserId:  int(req.UserId),
		WorksId: int(req.WorksId),
		Drop:    req.Drop,
	}
	err := services.CrateGiveThumbsUp(up)
	if err != nil {
		return &user1.AddGiveThumbsUpResp{
			Code: 400,
			Msg:  "创建失败",
		}, err
	}
	resp := &user1.AddGiveThumbsUpResp{
		Code: 200,
		Msg:  "创建成功",
	}
	return resp, err
}

func (s *UserServer) DeletedGiveThumbsUp(ctx context.Context, req *user1.DeletedGiveThumbsUpReq) (*user1.DeletedGiveThumbsUpResp, error) {
	err := services.DeletedGiveThumbsUp(int(req.Id))
	if err != nil {
		return &user1.DeletedGiveThumbsUpResp{
			Code: 400,
			Msg:  "删除失败",
		}, err
	}
	resp := &user1.DeletedGiveThumbsUpResp{
		Code: 200,
		Msg:  "删除成功",
	}
	return resp, err
}

func (s *UserServer) AddDiscuss(ctx context.Context, req *user1.AddDiscussReq) (*user1.AddDiscussResp, error) {
	discuss := dao.Discuss{
		UserId:  int(req.UserId),
		WorksId: int(req.WorksId),
		Content: req.Content,
	}
	err := services.CreateDiscuss(discuss)
	if err != nil {
		return &user1.AddDiscussResp{
			Code: 400,
			Msg:  "添加失败",
		}, err
	}
	resp := &user1.AddDiscussResp{
		Code: 200,
		Msg:  "添加成功",
	}
	return resp, err
}
